package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.NewsDetail;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.service.ICollectService;
import cn.wolfcode.trip.base.service.INewsDetailService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private INewsDetailService newsDetailService;
    @Autowired
    private ICollectService collectService;
    /**
     * 查询个人收藏的内容
     * @param qo
     * @return
     */
    @GetMapping
    public Object getCollect(@ModelAttribute("qo") CollectQueryObject qo){
        PageInfo pageInfo = newsDetailService.selectCollect(qo);
        return pageInfo;

    }

    /**
     * 收藏操作
     * @param userId
     * @param collectId
     * @param type
     * @return
     */
    @PostMapping
    public Object IsCollect(Long userId,Long collectId,int type){
        //指定是否有收藏
        Collect collect = collectService.selectByIdAndType(userId, collectId, type);
        if (collect != null){
            //删除收藏
            collectService.deleteByUserIdAndType(userId,collectId,type);
        }else {
            //添加收藏
            collectService.saveByUserIdAndType(userId,collectId,type);
        }
        return collect;
    }

    /**
     * 指定用户id,游记或者日报后者攻略的id 类型 查询收藏
     * @param userId
     * @param collectId
     * @param type
     * @return
     */
    @GetMapping("/one")
    public Object getCollectByUserIdAndCollectIdAndType(Long userId,Long collectId,int type){
        return collectService.selectByIdAndType(userId,collectId,type);
    }

    /**
     * 指定指定内容和类型查询收藏数量
     * @param collectId
     * @param type
     * @return
     */
    @GetMapping("/count")
    public Object GetCollectCount(Long collectId,int type){
        int count = collectService.count(collectId, type);
        return count;

    }
}
